package com.lanhe.app.controller.order;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson2.JSON;
import com.lanhe.common.constant.Constants;
import com.lanhe.common.core.controller.BaseController;
import com.lanhe.common.core.domain.AjaxResult;
import com.lanhe.common.core.page.TableDataInfo;
import com.lanhe.common.utils.StringUtils;
import com.lanhe.common.utils.qingchuang.dto.QingChuangOrderDTO;
import com.lanhe.order.domain.LhOrdersQingchuang;
import com.lanhe.order.domain.dto.LhOrdersQingchuangDto;
import com.lanhe.order.service.ILhOrdersQingchuangService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 清创订单
 *
 * @author zhanggl
 * @date 2023/12/15
 */
@RestController
@RequestMapping("/api/qingchuang/orders")
@Slf4j
public class ApiLhOrdersQingChuangController extends BaseController {

	@Autowired
	ILhOrdersQingchuangService ordersQingchuangService;

	/**
	 * 分页查询所有数据
	 */
	@GetMapping("/list")
	public TableDataInfo list(LhOrdersQingchuang ordersQingchuang) {
		startPage();
		ordersQingchuang.setMemberId(getUserId());
		List<LhOrdersQingchuang> list = ordersQingchuangService.selectOrdersList(ordersQingchuang);
		for (LhOrdersQingchuang lhOrdersQingchuang : list) {
			lhOrdersQingchuang.setStatusName(Constants.QingchuangOrderStatus.getQingchuangOrderStatusName(lhOrdersQingchuang.getStatus()));
		}
		return getDataTable(list);
	}


	/**
	 * 新增数据
	 *
	 * @param qingChuangOrderDTO DTO
	 * @return 新增结果
	 */
	@PostMapping("/add")
	public AjaxResult add(@RequestBody QingChuangOrderDTO qingChuangOrderDTO) throws Exception {
		if (StringUtils.isBlank(qingChuangOrderDTO.getNumber())) {
			return error("订单号不能为空");
		}
		qingChuangOrderDTO.setMemberId(getUserId());
		qingChuangOrderDTO.setUid(getLoginUser().getUserKey());
		qingChuangOrderDTO.setMobile(getLoginUser().getLhMemberBase().getMobile());
		ordersQingchuangService.insertOrdersQingchuang(qingChuangOrderDTO);
		return success();
	}


	/**
	 * 订单结算数据回调
	 *
	 * @param ordersQingchuangDto 实体对象
	 * @return 结果
	 */
	@PostMapping("/settlementNotice")
	public Object settlementNotice(LhOrdersQingchuangDto ordersQingchuangDto) throws Exception {
		log.info("ApiLhOrdersQingChuangController--->settlementNotice 清创：【{}】", JSON.toJSONString(ordersQingchuangDto));
		if (ObjectUtil.isNull(ordersQingchuangDto)) {
			return toAjaxDuoyou(200);
		}
		ordersQingchuangService.settlementNotice(ordersQingchuangDto);
		return toAjaxDuoyou(200);
	}

}

